package com.seari.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.seari.mapper.TicketingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName TicketingServiceImpl
 * @Description TODO
 * @Author LANDSUS
 * @Date 2021/5/12 20:17
 * @Version 1.0
 */
@DS("yidian")
@Service
public class TicketingServiceImpl implements TicketingService{

    @Autowired
    private TicketingMapper mapper;

    @Override
    public Map<String, String> getTicketing() {
        Map<String,String> finalMap = new LinkedHashMap<>();
        Map<String, Object> data = mapper.getTicketing();
        BigDecimal zfb = new BigDecimal(data.get("ZFB") != null ? data.get("ZFB").toString():"0");
        BigDecimal yl = new BigDecimal(data.get("YL") != null ? data.get("YL").toString():"0");
        BigDecimal wx = new BigDecimal(data.get("WX") != null ? data.get("WX").toString():"0");
        BigDecimal pCard = new BigDecimal(data.get("P_CARD") != null ? data.get("P_CARD").toString():"0");
        BigDecimal sCard = new BigDecimal(data.get("S_CARD") != null ? data.get("S_CARD").toString():"0");
        BigDecimal jtb = new BigDecimal(data.get("JTB") != null ? data.get("JTB").toString():"0");
        BigDecimal tick = new BigDecimal(data.get("TICK") != null ? data.get("TICK").toString():"0");
        BigDecimal gwp = new BigDecimal(data.get("GWP") != null ? data.get("GWP").toString():"0");
        BigDecimal other = new BigDecimal(data.get("OTHER") != null ? data.get("OTHER").toString():"0");

        BigDecimal total = zfb.add(yl).add(wx).add(pCard).add(sCard).add(jtb).add(tick).add(gwp).add(other);
        BigDecimal ewm = zfb.add(yl).add(wx).divide(total, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(0);
        BigDecimal sjjtk = sCard.divide(total, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(0);
        BigDecimal ptjtk = pCard.divide(total, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(0);
        BigDecimal dcp = tick.divide(total, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")).setScale(0);
        finalMap.put("二维码", ewm.toString()+"%");
        finalMap.put("单程票", dcp.toString()+"%");
        finalMap.put("手机交通卡", sjjtk.toString()+"%");
        finalMap.put("普通交通卡", ptjtk.toString()+"%");
        finalMap.put("其他", new BigDecimal("100").subtract(ewm).subtract(sjjtk).subtract(ptjtk).subtract(dcp).setScale(0).toString()+"%");
        return finalMap;
    }
}
